package com.cslc.wechat.createmenu.service;


import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cslc.wechat.common.dto.menu.Menu;
import com.cslc.wechat.common.exception.WeChatException;
import com.cslc.wechat.common.mp.MpApi;
import com.cslc.wechat.util.WeChatUtil;

public class CreateMenuService {
	private static Logger logger = Logger.getLogger(CreateMenuService.class.getName());
	
	public JSONObject menuCreate(Menu menu) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.CREATE_MENU.getUrl(), access_token);
		String requestMethod = MpApi.CREATE_MENU.getMethod();
		String postData = JSONObject.fromObject(menu).toString();
		logger.debug("自定义菜单请求url：" + requestUrl);
		logger.debug("自定义菜单请求POST内容：" + postData);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}


	public JSONObject menuCreate(String fileName) {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.CREATE_MENU.getUrl(), access_token);
		String requestMethod = MpApi.CREATE_MENU.getMethod();
		//从菜单配置文件读取字符串，再进行json格式化
		String postData = JSONObject.fromObject(WeChatUtil.readFile(fileName)).toString();
		logger.info("自定义菜单请求url：" + requestUrl);
		logger.info("自定义菜单请求POST内容：" + postData);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.info("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}	

	public JSONObject menuGet() {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.GET_MENU.getUrl(), access_token);
		String requestMethod = MpApi.GET_MENU.getMethod();
		String postData = null;
		logger.debug("自定义菜单请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}

	public JSONObject menuDelete() {
		JSONObject jsonObject = null;
		String access_token = WeChatUtil.getAccessToken().getToken();
		String requestUrl = String.format(MpApi.DELETE_MENU.getUrl(), access_token);
		String requestMethod = MpApi.DELETE_MENU.getMethod();
		String postData = null;
		logger.debug("自定义菜单请求url：" + requestUrl);
		jsonObject = WeChatUtil.httpRequest(requestUrl, requestMethod, postData);
		logger.debug("自定义菜单请求返回包： " + jsonObject);
		try {
			WeChatUtil.isSuccess(jsonObject);
		} catch (WeChatException e) {
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	

}
